/*     */ package dm.jdbc.rowset;
/*     */ 
/*     */ import dm.jdbc.driver.DmdbConnection_bs;
/*     */ import dm.jdbc.driver.DmdbPreparedStatement_bs;
/*     */ import dm.jdbc.driver.DmdbResultSetMetaData_bs;
/*     */ import java.sql.SQLException;
/*     */ 
/*     */ public class DmdbCachedRowSetWriterInner
/*     */ {
/*  10 */   private StringBuffer m_updateCommand = null;
/*  11 */   private StringBuffer m_insertCommand = null;
/*  12 */   private StringBuffer m_deleteCommand = null;
/*  13 */   private int m_columnCount = 0;
/*  14 */   private String m_tabName = "";
/*  15 */   private DmdbResultSetMetaData_bs m_rsmd = null;
/*  16 */   private DmdbPreparedStatement_bs m_updateStmt = null;
/*  17 */   private DmdbPreparedStatement_bs m_insertStmt = null;
/*  18 */   private DmdbPreparedStatement_bs m_deleteStmt = null;
/*  19 */   private DmdbConnection_bs m_conn = null;
/*     */   
/*     */   private void initialize(DmdbCachedRowSet_bs caller) throws SQLException {
/*  22 */     this.m_rsmd = caller.getMetaDataInner();
/*  23 */     this.m_columnCount = this.m_rsmd.getColumnCount();
/*  24 */     this.m_conn = caller.getConnectionInner();
/*     */     
/*  26 */     this.m_tabName = caller.getTableName().toUpperCase();
/*  27 */     this.m_updateCommand = new StringBuffer("UPDATE " + this.m_tabName + " SET ");
/*  28 */     this.m_insertCommand = new StringBuffer("INSERT INTO " + this.m_tabName + "(");
/*  29 */     this.m_deleteCommand = new StringBuffer("DELETE FROM " + this.m_tabName + 
/*  30 */       " WHERE ");
/*  31 */     for (int i = 0; i < this.m_columnCount; i++) {
/*  32 */       if (i > 0) {
/*  33 */         this.m_updateCommand.append(" , ");
/*  34 */         this.m_insertCommand.append(" , ");
/*     */       }
/*     */       
/*  37 */       this.m_updateCommand.append(this.m_rsmd.getColumnName(i + 1) + " = ?");
/*  38 */       this.m_insertCommand.append(this.m_rsmd.getColumnName(i + 1));
/*     */     }
/*     */     
/*  41 */     this.m_insertCommand.append(") VALUES(");
/*  42 */     this.m_updateCommand.append(" WHERE ");
/*     */     
/*  44 */     for (int j = 0; j < this.m_columnCount; j++) {
/*  45 */       if (j > 0) {
/*  46 */         this.m_insertCommand.append(",");
/*  47 */         this.m_updateCommand.append(" AND ");
/*  48 */         this.m_deleteCommand.append(" AND ");
/*     */       }
/*     */       
/*  51 */       this.m_insertCommand.append("?");
/*  52 */       this.m_updateCommand.append(this.m_rsmd.getColumnName(j + 1) + "= ?");
/*  53 */       this.m_deleteCommand.append(this.m_rsmd.getColumnName(j + 1) + "= ?");
/*     */     }
/*  55 */     this.m_insertCommand.append(")");
/*     */     
/*  57 */     this.m_insertStmt = 
/*  58 */       ((DmdbPreparedStatement_bs)this.m_conn.prepareStatement(this.m_insertCommand.substring(0, 
/*  59 */       this.m_insertCommand.length())));
/*     */     
/*  61 */     this.m_updateStmt = 
/*  62 */       ((DmdbPreparedStatement_bs)this.m_conn.prepareStatement(this.m_updateCommand.substring(0, 
/*  63 */       this.m_updateCommand.length())));
/*     */     
/*  65 */     this.m_deleteStmt = 
/*  66 */       ((DmdbPreparedStatement_bs)this.m_conn.prepareStatement(this.m_deleteCommand.substring(0, 
/*  67 */       this.m_deleteCommand.length())));
/*     */   }
/*     */   
/*     */   private boolean insertRow(DmdbRow row) throws SQLException {
/*  71 */     this.m_insertStmt.clearParameters();
/*     */     
/*  73 */     for (int i = 0; i < this.m_columnCount; i++) {
/*  74 */       Object obj = row.getColumn(i);
/*  75 */       if (obj == null) {
/*  76 */         this.m_insertStmt.setNull(i + 1, this.m_rsmd.getColumnType(i + 1));
/*     */       } else {
/*  78 */         this.m_insertStmt.setObject(i + 1, obj);
/*     */       }
/*     */     }
/*  81 */     return 1 == this.m_insertStmt.executeUpdate();
/*     */   }
/*     */   
/*     */   private boolean updateRow(DmdbRow row) throws SQLException {
/*  85 */     if (row.isOriginalWithNull()) {
/*  86 */       return updateWithNull(row);
/*     */     }
/*  88 */     return updateNonNull(row);
/*     */   }
/*     */   
/*     */   private boolean updateWithNull(DmdbRow row) throws SQLException {
/*  92 */     StringBuffer updateSql = new StringBuffer("UPDATE " + this.m_tabName + 
/*  93 */       " SET ");
/*  94 */     StringBuffer updateSqlTail = new StringBuffer(" WHERE ");
/*     */     
/*  96 */     int[] tailCols = new int[this.m_columnCount];
/*  97 */     int tailCnt = this.m_columnCount;
/*  98 */     for (int i = 0; i < this.m_columnCount; i++) {
/*  99 */       if (i > 0) {
/* 100 */         updateSql.append(" , ");
/* 101 */         updateSqlTail.append(" AND ");
/*     */       }
/*     */       
/* 104 */       updateSql.append(this.m_rsmd.getColumnName(i + 1) + " = ?");
/*     */       
/* 106 */       updateSqlTail.append(this.m_rsmd.getColumnName(i + 1));
/* 107 */       if (row.isOriginalNull(i)) {
/* 108 */         tailCols[i] = -1;
/* 109 */         updateSqlTail.append(" IS NULL ");
/*     */       } else {
/* 111 */         tailCols[i] = (++tailCnt);
/* 112 */         updateSqlTail.append(" = ? ");
/*     */       }
/*     */     }
/*     */     
/* 116 */     updateSql.append(updateSqlTail);
/*     */     
/* 118 */     DmdbPreparedStatement_bs pstmt = (DmdbPreparedStatement_bs)this.m_conn
/* 119 */       .prepareStatement(updateSql.substring(0, updateSql.length()));
/*     */     
/* 121 */     for (int i = 0; i < this.m_columnCount; i++)
/*     */     {
/* 123 */       Object orgObj = row.getColumn(i);
/* 124 */       Object obj = row.isColumnChanged(i) ? row.getChangedColumn(i) : 
/* 125 */         orgObj;
/* 126 */       if (obj == null) {
/* 127 */         pstmt.setNull(i + 1, this.m_rsmd.getColumnType(i + 1));
/*     */       } else {
/* 129 */         pstmt.setObject(i + 1, obj);
/*     */       }
/*     */       
/* 132 */       if (tailCols[i] != -1) {
/* 133 */         pstmt.setObject(tailCols[i], orgObj);
/*     */       }
/*     */     }
/* 136 */     boolean sus = 1 == pstmt.executeUpdate();
/* 137 */     pstmt.close();
/* 138 */     updateSql = null;
/* 139 */     updateSqlTail = null;
/*     */     
/* 141 */     return sus;
/*     */   }
/*     */   
/*     */   private boolean updateNonNull(DmdbRow row) throws SQLException {
/* 145 */     for (int i = 0; i < this.m_columnCount; i++) {
/* 146 */       Object orgObj = row.getColumn(i);
/*     */       
/*     */ 
/* 149 */       Object obj = row.isColumnChanged(i) ? row.getChangedColumn(i) : 
/* 150 */         orgObj;
/* 151 */       if (obj == null) {
/* 152 */         this.m_updateStmt.setNull(i + 1, this.m_rsmd.getColumnType(i + 1));
/*     */       } else {
/* 154 */         this.m_updateStmt.setObject(i + 1, obj);
/*     */       }
/*     */       
/* 157 */       this.m_updateStmt.setObject(this.m_columnCount + i + 1, orgObj);
/*     */     }
/*     */     
/* 160 */     return 1 == this.m_updateStmt.executeUpdate();
/*     */   }
/*     */   
/*     */   private boolean deleteRow(DmdbRow row) throws SQLException {
/* 164 */     if (row.isOriginalWithNull()) {
/* 165 */       return deleteWithNull(row);
/*     */     }
/* 167 */     return deleteNonNull(row);
/*     */   }
/*     */   
/*     */   private boolean deleteWithNull(DmdbRow row) throws SQLException {
/* 171 */     StringBuffer deleteSql = new StringBuffer("DELETE FROM " + this.m_tabName + 
/* 172 */       " WHERE ");
/*     */     
/* 174 */     int[] tailCols = new int[this.m_columnCount];
/* 175 */     int tailCnt = 0;
/* 176 */     for (int i = 0; i < this.m_columnCount; i++) {
/* 177 */       if (i > 0) {
/* 178 */         deleteSql.append(" AND ");
/*     */       }
/* 180 */       if (row.isOriginalNull(i)) {
/* 181 */         deleteSql.append(this.m_rsmd.getColumnName(i + 1) + " IS NULL ");
/* 182 */         tailCols[i] = -1;
/*     */       } else {
/* 184 */         deleteSql.append(this.m_rsmd.getColumnName(i + 1) + " = ? ");
/* 185 */         tailCols[i] = (++tailCnt);
/*     */       }
/*     */     }
/*     */     
/* 189 */     DmdbPreparedStatement_bs pstmt = (DmdbPreparedStatement_bs)this.m_conn
/* 190 */       .prepareStatement(deleteSql.substring(0, deleteSql.length()));
/* 191 */     for (int i = 0; i < this.m_columnCount; i++) {
/* 192 */       if (tailCols[i] != -1) {
/* 193 */         pstmt.setObject(tailCols[i], row.getColumn(i));
/*     */       }
/*     */     }
/* 196 */     boolean sus = 1 == pstmt.executeUpdate();
/* 197 */     pstmt.close();
/* 198 */     pstmt = null;
/* 199 */     deleteSql = null;
/*     */     
/* 201 */     return sus;
/*     */   }
/*     */   
/*     */   private boolean deleteNonNull(DmdbRow row) throws SQLException {
/* 205 */     for (int i = 0; i < this.m_columnCount; i++) {
/* 206 */       this.m_deleteStmt.setObject(i + 1, row.getColumn(i));
/*     */     }
/* 208 */     return 1 == this.m_deleteStmt.executeUpdate();
/*     */   }
/*     */   
/*     */   private void release() throws SQLException {
/* 212 */     this.m_insertCommand = null;
/* 213 */     if ((this.m_insertStmt != null) && (!this.m_insertStmt.isClosed())) {
/* 214 */       this.m_insertStmt.close();
/* 215 */       this.m_insertStmt = null;
/*     */     }
/*     */     
/* 218 */     this.m_updateCommand = null;
/* 219 */     if ((this.m_updateStmt != null) && (!this.m_updateStmt.isClosed())) {
/* 220 */       this.m_updateStmt.close();
/* 221 */       this.m_updateStmt = null;
/*     */     }
/*     */     
/* 224 */     this.m_deleteCommand = null;
/* 225 */     if ((this.m_deleteStmt != null) && (!this.m_deleteStmt.isClosed())) {
/* 226 */       this.m_deleteStmt.close();
/* 227 */       this.m_deleteStmt = null;
/*     */     }
/*     */     
/* 230 */     this.m_columnCount = 0;
/* 231 */     this.m_rsmd = null;
/* 232 */     this.m_tabName = "";
/*     */   }
/*     */   
/*     */   public boolean writeData(DmdbCachedRowSet_bs caller) throws SQLException {
/* 236 */     initialize(caller);
/* 237 */     if (this.m_columnCount < 1) {
/* 238 */       release();
/* 239 */       return true;
/*     */     }
/*     */     
/* 242 */     boolean showDeleted = caller.getShowDeleted();
/*     */     
/*     */ 
/* 245 */     if (this.m_conn.getAutoCommit()) {
/* 246 */       this.m_conn.setAutoCommit(false);
/*     */     }
/*     */     
/* 249 */     caller.setShowDeleted(true);
/*     */     
/*     */ 
/* 252 */     caller.beforeFirst();
/*     */     
/*     */ 
/* 255 */     boolean insertRs = true;
/* 256 */     boolean updateRs = true;
/* 257 */     boolean deleteRs = true;
/* 258 */     while (caller.next()) {
/* 259 */       if (caller.rowInserted()) {
/* 260 */         if (!caller.rowDeleted()) {
/* 261 */           DmdbRow row = caller.getCurrentRow();
/* 262 */           insertRs = (insertRow(row)) && (insertRs);
/*     */         }
/* 264 */       } else if (caller.rowUpdated()) {
/* 265 */         DmdbRow row = caller.getCurrentRow();
/* 266 */         updateRs = (updateRow(row)) && (updateRs);
/* 267 */       } else if (caller.rowDeleted()) {
/* 268 */         DmdbRow row = caller.getCurrentRow();
/* 269 */         deleteRs = (deleteRow(row)) && (deleteRs);
/*     */       }
/*     */     }
/*     */     
/*     */ 
/* 274 */     if ((!insertRs) || (!updateRs) || (!deleteRs)) {
/* 275 */       this.m_conn.rollback();
/*     */     } else {
/* 277 */       this.m_conn.commit();
/*     */       
/*     */ 
/* 280 */       caller.setOriginal();
/*     */     }
/*     */     
/*     */ 
/* 284 */     caller.setShowDeleted(showDeleted);
/*     */     
/*     */ 
/* 287 */     release();
/*     */     
/*     */ 
/* 290 */     if ((!caller.keepConnecting()) && (this.m_conn != null) && (!this.m_conn.isClosed())) {
/* 291 */       this.m_conn.close();
/* 292 */       this.m_conn = null;
/*     */     }
/*     */     
/* 295 */     return true;
/*     */   }
/*     */ }


/* Location:              D:\Program Files\DBMS_20150813\jdbc\Dm7JdbcDriver17.jar!\dm\jdbc\rowset\DmdbCachedRowSetWriterInner.class
 * Java compiler version: 5 (49.0)
 * JD-Core Version:       0.7.1
 */